package com.example.myapplication;
import BundleMessage.BundleDataActivity1;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabWidget;

import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class TabNavActivity extends AppCompatActivity {
  TabHost tabHost;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Tab视图");
    setSupportActionBar(toolbar);
    SetTabNav();
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "跳转到上一个Activity");
    menu.add(0, 2, 1, "跳转到下一个Activity");
    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case 1:
        BackToLastActivity();
        break;
      case 2:
        GoToNextActivity();
        break;

    }
    return true;
  }

  private void BackToLastActivity() {
    Intent intent = new Intent(this, BundleDataActivity1.class);
    startActivity(intent);
  }

  private void GoToNextActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
  }

  private void SetText(int id, String userName) {
    String welcomeStr = getString(R.string.welcome_message, userName);
    TextView textView=(TextView) findViewById(id);
    textView.setText(welcomeStr);

  }

  private void SetTabNav() {
    tabHost = (TabHost) findViewById(R.id.tabHost);

    tabHost.setup();

    // Create TabSpec for tab1
    TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
    tab1.setContent(R.id.tab1); // Set the content layout resource id
    tab1.setIndicator("圈弟");
    SetText(R.id.tab1_text,"圈弟");
    tabHost.addTab(tab1);

    TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
    tab2.setContent(R.id.tab2);
    tab2.setIndicator("小哈");
    SetText(R.id.tab2_text,"小哈");
    tabHost.addTab(tab2);

    TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3");
    tab3.setContent(R.id.tab3);
    tab3.setIndicator("神奇哈姆金丝熊");
    SetText(R.id.tab3_text,"神奇哈姆金丝熊");
    tabHost.addTab(tab3);

    TabHost.TabSpec tab4 = tabHost.newTabSpec("tab4");
    tab4.setContent(R.id.tab4);
    tab4.setIndicator("银狐武神");
    SetText(R.id.tab4_text,"银狐武神");
    tabHost.addTab(tab4);
    SetTabWidget();
  }

  private void SetTabWidget() {
    TabWidget tabWidget = tabHost.getTabWidget();
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
      @Override
      public void onTabChanged(String tabId) {
        // Handle tab changed event
      }
    });
  }
}
